package com.isay.app.honey.ui.cash;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.cash.history.HistoryActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;

import org.json.JSONObject;

/**
 * 提现管理
 */
public class CashManagerActivity extends BaseActivity implements View.OnClickListener {


    private TextView mTvMyMoney;
    private TextView mTvBankCode;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CashManagerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cash);
        initView();
        //initData();
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        cashManager();
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_title)).setText("提现管理");
        findViewById(R.id.iv_go_back).setOnClickListener(this);
        findViewById(R.id.tv_cash_go).setOnClickListener(this);
        findViewById(R.id.lay_my_account).setOnClickListener(this);
        findViewById(R.id.lay_cash_detail).setOnClickListener(this);
        mTvMyMoney = findViewById(R.id.tv_cash_my_money);
        mTvBankCode = findViewById(R.id.tv_cash_bank_code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            case R.id.tv_cash_go:
                GoCashActivity.launch(this);
                break;
            case R.id.lay_my_account:
                BankCodeActivity.launch(this);
                break;
            case R.id.lay_cash_detail:
                HistoryActivity.launch(this);
                break;
            default:
                break;
        }
    }


    private void cashManager() {
        NetworkManager.cashManager(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    String money = jsonObject.optString("money");
                    String name = jsonObject.optString("name");
                    String bankcode = jsonObject.optString("bankcode");
                    mTvMyMoney.setText(money);
                    setNameBankCode(name, bankcode);
                } catch (Exception e) {
                    onFailure(e);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && requestCode == 200 && data != null) {
            String name = data.getStringExtra("name");
            String bankcode = data.getStringExtra("bankcode");
            setNameBankCode(name, bankcode);
        }
    }

    private void setNameBankCode(String name, String bankCode) {
        mTvBankCode.setText(name + "   " + bankCode + " >  ");
    }

}
